package frontend.login.controller;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.Map;
import java.util.Properties;
import javax.mail.Message;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

import org.apache.struts2.interceptor.SessionAware;

import com.opensymphony.xwork2.ActionSupport;
import core.profile.model.BasicCredential;
import frontend.registration.controller.Encript;
import frontend.registration.dao.RegisterDao;
import frontend.user.controller.GetDataByEmailId;

public class ForgetPasswordAction extends ActionSupport implements SessionAware{
	
	private static final long serialVersionUID = 5169101237999041920L;
	BasicCredential basicCredential ;
	Map<String, Object> session ;
	static String path = "source"+File.separatorChar+"smtpConfig.properties";
	private String password;
	private String newPassword ;
	private String conformPassword ;
	private static Properties props = new Properties();
	
	public ForgetPasswordAction()
	{
		
	}
	public String getNewPassword() {
		return newPassword;
	}


	public void setNewPassword(String newPassword) {
		this.newPassword = newPassword;
	}

	public String getConformPassword() {
		return conformPassword;
	}


	public void setConformPassword(String conformPassword) {
		this.conformPassword = conformPassword;
	}


	public String getPassword() {
		return password;
	}


	public void setPassword(String password) {
		this.password = password;
	}
	
	
	public BasicCredential getBasicCredential()
	{
		return this.basicCredential ;
	}
   
	public void setBasicCredential(BasicCredential basicCredential) {
		this.basicCredential = basicCredential;
	}

	public String changePassword()
	{
		this.basicCredential=GetDataByEmailId.getBasicCredentialByUserId(session.get("emailId").toString());
		if((basicCredential!=null)&(Encript.doEncript(this.getPassword()).equals(basicCredential.getPassword())))
		{
			if(this.getConformPassword().equals(this.getNewPassword()))
			{
				basicCredential.setPassword(Encript.doEncript(this.getNewPassword()));
				RegisterDao.setPassword(basicCredential);
				
			}
			return SUCCESS;
		}
		else
		{
			return ERROR;
		}
		
	}
	
	public static boolean sendLinkForPasswordChange()
	{
		boolean status=true;
		
		try{
			ForgetPasswordAction fpa= new ForgetPasswordAction();
			fpa.loadProps2();
			
           String host = props.getProperty("mail.smtp.host");  
           String port1 = props.getProperty("mail.smtp.socketFactory.port");  
           String  auth = props.getProperty("mail.smtp.auth");
           String  SSLSocketFactory = props.getProperty("mail.smtp.socketFactory.class");  
           String  port2 = props.getProperty("mail.smtp.port");  
           final String  username = props.getProperty("username");  
           final String  password = props.getProperty("password");  
            props.put("mail.smtp.host", host);
       		props.put("mail.smtp.socketFactory.port", port1);
       		props.put("mail.smtp.socketFactory.class",SSLSocketFactory);
       		props.put("mail.smtp.auth", auth);
       		props.put("mail.smtp.port", port2);
    		Session session = Session.getDefaultInstance(props,
    				new javax.mail.Authenticator() {
    					protected PasswordAuthentication getPasswordAuthentication() {
    						return new PasswordAuthentication(username,password);
    					}
    				});
    		
    		Message message = new MimeMessage(session);
			message.setFrom(new InternetAddress("aaa@adv.com"));
			message.setRecipients(Message.RecipientType.TO,
					InternetAddress.parse("atpinkel@gmail.com"));
			message.setSubject("Change Password");
			message.setText("Dear Mail Crawler," +
					"\n\n No spam to my email, please!");
 
			Transport.send(message);
 
			System.out.println("Done");
			return true;
        /* code to use values read from the file*/  
         }catch(Exception e){  
           System.out.println("Failed to read from " + path + " file.");
           return false;
         } 
	}
	
	public void loadProps2() {
		System.out.println(path);
        InputStream in = this.getClass().getClassLoader().getResourceAsStream(path);
        try {
        	props.load(in);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
	
	public String execute() throws Exception{
    	this.basicCredential=GetDataByEmailId.getBasicCredentialByUserId(session.get("emailId").toString());
    	if((basicCredential != null)&&(true==ForgetPasswordAction.sendLinkForPasswordChange()))
    	{
    		  		
    		return SUCCESS;
    	}
    	else{
    		return ERROR;
    	}
    }
	@Override
	public void setSession(Map<String, Object> s) {
		// TODO Auto-generated method stub
		this.session = s ;
	}

}
